package com.tank.factory;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.entity.Spawns;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.tank.components.BaseComponent;
import com.tank.constant.Config;
import com.tank.constant.GameType;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

public class MapEntityFactory implements EntityFactory {

    @Spawns("brick")
    public Entity newBrick(SpawnData data) {
        return FXGL.entityBuilder(data)
                .type(GameType.BRICK)
                .viewWithBBox("map/brick.png")
                .bbox(BoundingShape.box(Config.CELL_SIZE, Config.CELL_SIZE))
                .collidable()
                .neverUpdated()
                .build();
    }

    @Spawns("stone")
    public Entity newStone(SpawnData data) {
        return FXGL.entityBuilder(data)
                .type(GameType.STONE)
                .viewWithBBox("map/stone.png")
                .bbox(BoundingShape.box(Config.CELL_SIZE, Config.CELL_SIZE))
                .collidable()
                .neverUpdated()
                .build();
    }

    @Spawns("greens")
    public Entity newGreens(SpawnData data) {
        int width = data.get("width");
        int height = data.get("height");
        return FXGL.entityBuilder(data)
                .type(GameType.GREENS)
//                .viewWithBBox("map/greens.png")
                .bbox(BoundingShape.box(width, height))
                .collidable()
                .neverUpdated()
                .zIndex(1000)
                .build();
    }

    @Spawns("snow")
    public Entity newSnow(SpawnData data) {
        int width = data.get("width");
        int height = data.get("height");
        return FXGL.entityBuilder(data)
                .type(GameType.SNOW)
//                .viewWithBBox("map/snow.png")
                .bbox(BoundingShape.box(width, height))
                .collidable()
                .neverUpdated()
                .build();
    }

    @Spawns("sea")
    public Entity newSea(SpawnData data) {
        AnimationChannel ac = new AnimationChannel(FXGL.image("map/sea_anim.png"), Duration.seconds(0.8), 2);
        AnimatedTexture at = new AnimatedTexture(ac);
        return FXGL.entityBuilder(data)
                .type(GameType.SEA)
                .collidable()
                .viewWithBBox(at.loop())
                .build();
    }

    @Spawns("border")
    public Entity newBorder(SpawnData data) {
        int width = data.get("width");
        int height = data.get("height");
        return FXGL.entityBuilder(data)
                .type(GameType.BORDER)
                .viewWithBBox(new Rectangle(width, height, Color.LIGHTGRAY))
                .collidable()
                .neverUpdated()
                .build();
    }

    @Spawns("base")
    public Entity newBase(SpawnData data) {
        int width = data.get("width");
        int height = data.get("height");
        return FXGL.entityBuilder(data)
                .type(GameType.BASE)
//                .viewWithBBox("map/flag.png")
                .bbox(BoundingShape.box(width, height))
                .with(new BaseComponent())
                .collidable()
                .neverUpdated()
                .build();
    }

    @Spawns("enemyspawn")
    public Entity newEnemySpawn(SpawnData data) {
        return FXGL.entityBuilder(data)
                .type(GameType.ENEMY_SPAWN)
                .neverUpdated()
                .build();
    }

    @Spawns("playerspawn")
    public Entity newPlayerSpawn(SpawnData data) {
        return FXGL.entityBuilder(data)
                .type(GameType.PLAYER_SPAWN)
                .neverUpdated()
                .build();
    }
}
